package com.tianyi.application.service;

import com.tianyi.application.mapper.MyUserDtoMapper;
import com.tianyi.application.dto.MyUserDto;
import com.tianyi.domain.entity.MyUserEntity;
import com.tianyi.domain.services.IMyUserService;
import com.tianyi.exception.ApplicationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MyUserApplicationSerivce implements IMyUserApplicationService{
    @Autowired
    IMyUserService iMyUserService;

    /**
     * @param myUserDto 入参
     * @Descripton 增加了对象的转换方法
     * @return dto对象
     */
    @Override
    public MyUserDto saveUser(MyUserDto myUserDto) throws Exception {
        if(find(myUserDto.getUserName()).size() > 0){
            throw  new ApplicationException(400,"重复的数据");
        }
        MyUserEntity myUser = iMyUserService.insertMyUser(MyUserDtoMapper.INSTANCE.myUserDto2Engity(myUserDto));
        return MyUserDtoMapper.INSTANCE.myUserEntity2Dto(myUser);
    }

    /**
     * @param userName
     * @return
     */
    @Override
    public List<MyUserDto> find(String userName) {
       List<MyUserEntity> myUserEntitys = iMyUserService.find(userName);
       return MyUserDtoMapper.INSTANCE.myUserEntitys2Dto(myUserEntitys);
    }
}
